import request from '../../../utils/request'
Page({
  data: {
    medicineList: [
      {
        id: 1,
        name: '阿司匹林',
        timeList: ['10:30'],
        dosage: '1',
        unit: '片',
        reminderDay: '每隔1天',
        isEnabled: true
      },
      {
        id: 2,
        name: '布洛芬',
        timeList: ['13:30', '20:30'],
        dosage: '1',
        unit: '片',
        reminderDay: '每天',
        isEnabled: true
      }
    ]
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    });
  },
  toggleReminder(e) {
    const index = e.currentTarget.dataset.index;
    const medicineList = this.data.medicineList;
    medicineList[index].isrun =!medicineList[index].isrun;
    this.setData({
      medicineList
    });
  },
  addMedicineReminder() {
    wx.navigateTo({
      url: '/pages/index/medicineReminder/addMedicine/addMedicine' // 假设新增提醒页面路径为 /pages/addMedicine/addMedicine
    });
  },
  selectTiXing(){
    let that=this
    request({
      url: 'tixing/tixing/list',
      method:'GET',
      success:(res)=>{
        console.log(res.data)
        that.setData({
          medicineList:res.data.rows
        })
      }
    })
  },
  onLoad(){
    this.selectTiXing()
  },
  onShow() {
    // 页面显示时刷新列表
    this.selectTiXing();
    this.loadMedicineList();
  },
  loadMedicineList() {
    // 从服务器或本地存储获取最新的药品列表
    // 这里暂时使用模拟数据
  },
  onMedicineClick(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `./editMedicine/editMedicine?id=`+id
    });
  },
  onSwitchChange(e) {
    const { id } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    // 更新开关状态
    const medicineList = this.data.medicineList.map(item => {
      if (item.id === id) {
        return { ...item, isrun: value };
      }
      return item;
    });

    this.setData({ medicineList });
  },
  onSwitchTap(e) {
    // 阻止冒泡，防止触发卡片点击
    e.stopPropagation();
  },
  onAddMedicine() {
    wx.navigateTo({
      url: './addMedicine/addMedicine'
    });
  }
});